jQuery.fn.extend({
    listerTreeRender: function() {
        this.each(function() {
            $(this).html(" ");
            new jQuery.ListerTree(this)
        })
    },
    listerTreeSelectValue: function(e) {
        var b = $(this).data("data");
        var a = e.split(",");
        if (b.fromList) {
            for (var d = 0; d < a.length; d++) {
                var c = -1;
                $.each(b.fromList, 
                function(f, g) {
                    if (g.id == a[d]) {
                        if (b.toList) {
                            b.toList.push(b.fromList[f]);
                            c = f
                        }
                    }
                });
                if (c != -1) {
                    b.fromList.splice(c, 1)
                }
            }
        }
        $(this).data("data", b);
        $(this).html("");
        new jQuery.ListerTree(this)
    },
    listerTreeUnSelectValue: function(e) {
        var b = $(this).data("data");
        var a = e.split(",");
        if (b.toList) {
            for (var d = 0; d < a.length; d++) {
                var c = -1;
                $.each(b.toList, 
                function(f, g) {
                    if (g.id == a[d]) {
                        if (b.fromList) {
                            b.fromList.push(b.toList[f]);
                            c = f
                        }
                    }
                });
                if (c != -1) {
                    b.toList.splice(c, 1)
                }
            }
        }
        $(this).data("data", b);
        $(this).html("");
        new jQuery.ListerTree(this)
    },
    listerTreeSetValue: function(f) {
        var b = $(this).data("data");
        var a = f.split(",");
        if (b.toList) {
            var e = b.toList.length;
            for (var d = 0; d < e; d++) {
                $.each(b.toList, 
                function(g, h) {
                    if (g == d) {
                        if (b.fromList) {
                            b.fromList.push(b.toList[g])
                        }
                    }
                })
            }
            b.toList.splice(0, e)
        }
        if (b.fromList) {
            for (var d = 0; d < a.length; d++) {
                var c = -1;
                $.each(b.fromList, 
                function(g, h) {
                    if (h.id == a[d]) {
                        if (b.toList) {
                            b.toList.push(b.fromList[g]);
                            c = g
                        }
                    }
                });
                if (c != -1) {
                    b.fromList.splice(c, 1)
                }
            }
        }
        $(this).data("data", b);
        $(this).html("");
        new jQuery.ListerTree(this)
    },
    listerTreeAddItem: function(a) {
        this.each(function() {
            var b = $(this).data("data");
            b.fromList.push(a);
            $(this).data("data", b);
            $(this).html("");
            new jQuery.ListerTree(this)
        })
    },
    listerTreeRemoveItem: function(a) {
        this.each(function() {
            var b = $(this).data("data");
            var c = -1;
            $.each(b.fromList, 
            function(d, e) {
                if (e.id.toString() == a) {
                    c = d
                }
            });
            if (c != -1) {
                b.fromList.splice(c, 1)
            }
            $(this).data("data", b);
            $(this).html("");
            new jQuery.ListerTree(this)
        })
    }
});
var listerTree_id = 1;
jQuery.ListerTree = function(a) {
    listerTree_id++;
    var l = C();
    var g = true;
    if ($(a).attr("disabled") != null) {
        if ($(a).attr("disabled") == "false" || $(a).attr("disabled") == false) {
            g = true
        } else {
            g = false
        }
    }
    var s = $(a);
    var L = $('<ul class="ztree dbSelectionMode"></ul>');
    var j = $('<ul class="ztree dbSelectionMode"></ul>');
    L.attr("id", "listerTree" + listerTree_id + "_from");
    j.attr("id", "listerTree" + listerTree_id + "_to");
    if ($(a).attr("listerHeight")) {
        L.height(Number($(a).attr("listerHeight")));
        j.height(Number($(a).attr("listerHeight")))
    } else {
        L.height(200);
        j.height(200)
    }
    var w = $("<div></div>");
    var x = $("<div></div>");
    if ($(a).attr("fromTitle") != null) {
        w.html($(a).attr("fromTitle"))
    } else {
        w.html("未选列表")
    }
    if ($(a).attr("toTitle") != null) {
        x.html($(a).attr("toTitle"))
    } else {
        x.html("已选列表")
    }
    var G = $("<div></div>").append(w).append(L);
    var F = $("<div></div>").append(x).append(j);
    if ($(a).attr("listerWidth")) {
        G.width(Number($(a).attr("listerWidth")));
        F.width(Number($(a).attr("listerWidth")))
    } else {
        G.width(200);
        F.width(200)
    }
    var E = $('<div class="listBtn"></div>');
    var m = $('<input type="button" value="全选&gt;&gt;" class="button"/>');
    m.bind("click", 
    function() {
        H()
    });
    E.append(m);
    E.append("<br/><br/>");
    var k = $('<input type="button" value="&lt;&lt;还原" class="button">');
    k.bind("click", 
    function() {
        I()
    });
    E.append(k);
    var M = $('<table cellspacing="0" cellpadding="0" style="border-style:none;"><tr><td class="ali01" style="border-style:none;padding:0;margin:0;"></td><td class="ali02" style="border-style:none;padding-left:5px;padding-right:5px;margin:0;"></td><td class="ali01" style="border-style:none;padding:0;margin:0;"></td></tr></table>');
    M.find("td").eq(0).append(G);
    M.find("td").eq(1).append(E);
    M.find("td").eq(2).append(F);
    $(s).append(M);
    $(s).width(M.width());
    if (g == false) {
        m.attr("disabled", "true");
        k.attr("disabled", "true");
        $(s).mask("组件被禁用", 0, false, "#ffffff")
    }
    m.buttonInputRender();
    k.buttonInputRender();
    $(s).append(l);
    var A = "-1";
    if ($(a).attr("selectedValue") != null) {
        A = $(a).attr("selectedValue")
    }
    var i = $(a).attr("params");
    var y;
    if (i) {
        try {
            y = JSON.parse(i)
        } catch(J) {
            y = "";
            alert("树形双选器参数格式有误！（提示：json数据key与value必须以双引号包围）")
        }
    } else {
        y = ""
    }
    var c = "";
    var D = $(a).attr("url");
    var v = $(a).attr("data");
    var h = $(a).data("data");
    if (h) {
        c = h;
        if (A == "-1") {
            z(h)
        } else {
            z(t(h, A))
        }
    } else {
        if (v) {
            try {
                c = JSON.parse(v)
            } catch(J) {
                c = "";
                alert("树形双选器参数格式有误！（提示：json数据key与value必须以双引号包围）")
            }
            if (A == "-1") {
                z(c)
            } else {
                z(t(c, A))
            }
            $(a).data("data", c)
        } else {
            if (D) {
                $.ajax({
                    url: $(a).attr("url"),
                    dataType: "json",
                    data: y,
                    error: function() {
                        alert("树形双选器数据源出错，请检查url路径")
                    },
                    success: function(e) {
                        c = e;
                        $(a).data("data", e);
                        if (A == "-1") {
                            z(e)
                        } else {
                            z(t(e, A))
                        }
                    }
                })
            }
        }
    }
    function t(N, R) {
        if (!N) {
            return
        }
        var e = R.split(",");
        if (N.toList) {
            var Q = N.toList.length;
            for (var P = 0; P < Q; P++) {
                $.each(N.toList, 
                function(S, T) {
                    if (S == P) {
                        if (N.fromList) {
                            N.fromList.push(N.toList[S])
                        }
                    }
                })
            }
            N.toList.splice(0, Q)
        }
        if (N.fromList) {
            for (var P = 0; P < e.length; P++) {
                var O = -1;
                $.each(N.fromList, 
                function(S, T) {
                    if (T.id == e[P]) {
                        if (N.toList) {
                            N.toList.push(N.fromList[S]);
                            O = S
                        }
                    }
                });
                if (O != -1) {
                    N.fromList.splice(O, 1)
                }
            }
        }
        return N
    }
    var b;
    var f;
    function r() {
        b = {
            view: {
                addHoverDom: p,
                removeHoverDom: n,
                selectedMulti: false
            },
            edit: {
                enable: true,
                showRemoveBtn: false,
                showRenameBtn: false,
                drag: {
                    isCopy: false,
                    prev: false,
                    inner: false,
                    next: false
                }
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                beforeDrag: B,
                beforeDrop: d,
                onDrop: o
            }
        };
        f = {
            edit: {
                enable: true,
                showRenameBtn: false,
                drag: {
                    isCopy: false,
                    prev: false,
                    next: false
                }
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                beforeDrag: B,
                beforeDrop: d,
                beforeRemove: q
            }
        }
    }
    function K(e) {
        if (!e) {
            return
        }
        if (e.fromList) {
            $.fn.zTree.init(L, b, e.fromList)
        }
        if (e.toList) {
            $.fn.zTree.init(j, f, e.toList)
        }
        $(a).attr("relValue", u());
        l.val(u())
    }
    function z(e) {
        if (!e) {
            return
        }
        r();
        K(e)
    }
    function C() {
        var e = $('<input type="hidden">');
        if ($(a).attr("name") != null) {
            e.attr("name", $(a).attr("name"))
        }
        return e
    }
    function B(P, O) {
        for (var N = 0, e = O.length; N < e; N++) {
            if (O[N].drag == false || O[N].drag == "false") {
                return false
            }
        }
        return true
    }
    function d(O, N, P, e) {
        return P ? P.drop !== false: true
    }
    function o(N, P, O, Q, e) {
        $(a).attr("relValue", u());
        l.val(u());
        $(a).trigger("itemClick")
    }
    function p(Q, P) {
        var S = $.fn.zTree.getZTreeObj(L.attr("id"));
        var R = $.fn.zTree.getZTreeObj(j.attr("id"));
        var e = $("#" + P.tId + "_span");
        if (P.oldParentId == null || P.oldParentId == "null" || P.drag == "false" || P.drag == false || P.editNameFlag || $("#addBtn_" + P.id).length > 0) {
            return
        }
        var N = "<button type='button' class='add' id='addBtn_" + P.id + "' title='选中' onfocus='this.blur();'></button>";
        e.append(N);
        var O = $("#addBtn_" + P.id);
        if (O) {
            O.bind("click", 
            function() {
                S.removeNode(P);
                R.addNodes(null, {
                    id: P.id,
                    parentId: P.parentId,
                    name: P.name,
                    drop: false,
                    icon: P.icon,
                    oldParentId: P.oldParentId
                });
                $(a).attr("relValue", u());
                l.val(u());
                $(a).trigger("itemClick");
                return false
            })
        }
    }
    function n(N, e) {
        $("#addBtn_" + e.id).unbind().remove()
    }
    function q(N, e) {
        var O = $.fn.zTree.getZTreeObj(j.attr("id"));
        O.selectNode(e);
        setTimeout(function() {
            O.removeNode(e);
            var Q = $.fn.zTree.getZTreeObj(L.attr("id"));
            var P = Q.getNodeByParam("id", e.oldParentId);
            if (P) {
                Q.addNodes(P, {
                    id: e.id,
                    parentId: P.id,
                    name: e.name,
                    drop: false,
                    icon: e.icon,
                    oldParentId: e.oldParentId
                })
            }
            $(a).attr("relValue", u());
            l.val(u());
            $(a).trigger("itemClick")
        },
        100);
        return false
    }
    function H() {
        if (!c) {
            return
        }
        $.fn.zTree.init(L, b, c.fromList);
        $.fn.zTree.init(j, f, c.toList);
        var P = $.fn.zTree.getZTreeObj(L.attr("id"));
        var O = $.fn.zTree.getZTreeObj(j.attr("id"));
        for (var e = 0; e < c.fromList.length; e++) {
            if (c.fromList[e].oldParentId) {
                if (c.fromList[e].drag != false && c.fromList[e].drag != "false") {
                    var N = P.getNodeByParam("id", c.fromList[e].id);
                    P.removeNode(N);
                    O.addNodes(null, c.fromList[e])
                }
            }
        }
        $(a).attr("relValue", u());
        l.val(u());
        $(a).trigger("itemClick")
    }
    function I() {
        if (!c) {
            return
        }
        K(t(c, ""));
        $(a).trigger("itemClick")
    }
    function u() {
        var P = $.fn.zTree.getZTreeObj(j.attr("id"));
        if (!P) {
            return
        }
        var e = P.getNodes();
        var O = "";
        for (var N = 0; N < e.length; N++) {
            O += "," + e[N].id
        }
        if (O.length > 0) {
            O = O.substring(1)
        }
        return O
    }
    return this
};